package com.dong.pointviewpager.widget

import android.content.Context
import android.util.AttributeSet
import com.dong.pointviewpager.config.GalleryViewPagerConfig
import com.dong.pointviewpager.interfaces.IPagerBean
import com.dong.pointviewpager.transformer.GalleryTransformer
import com.dong.pointviewpager.utils.DensityUtils.dp2px

/**
 * Created by Dong on 2018/3/13.
 */
class PointGalleryViewPager <T: IPagerBean> @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) : PointViewPager<T>(context, attrs, defStyleAttr) {

    private var config = GalleryViewPagerConfig(context)

    fun setPageWidth(pageWidth: Int){
        config.pageWidth = if(pageWidth > 0) dp2px(context, pageWidth.toFloat()).toInt() else pageWidth
    }

    fun setPageHeight(pageHeight: Int) {
        config.pageHeight = if(pageHeight > 0) dp2px(context, pageHeight.toFloat()).toInt() else pageHeight
    }

    fun setPageAlpha(pageAlpha: Float) {
        config.pageAlpha = pageAlpha
    }

    fun setPageScale(pageScale: Float) {
        config.pageScale = pageScale
    }

    fun setPageDistance(pageDistance: Float) {
        config.pageDistance = pageDistance
    }

    fun setPageRotation(pageRotation: Float) {
        config.pageRotation = pageRotation
    }

    /*
     * 初始化LoopViewPager
     */
    override fun initViewPager() {
        clipChildren = false
        loopViewPager = LoopViewPager<T>(context).apply {
            onPagerStateChangedListener = this@PointGalleryViewPager
            layoutParams = LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT).apply { addRule(CENTER_IN_PARENT) }
            clipChildren = false
            isPageCache = true
        }
        addView(loopViewPager)
    }

    fun initialise() {
        loopViewPager?.run {
            layoutParams.width = config.pageWidth
            layoutParams.height = config.pageHeight
            setPageTransformer(true, GalleryTransformer(config.pageAlpha, config.pageScale, config.pageDistance, config.pageRotation))
        }
    }
}